package com.qf.hospital.system.shiro;

import com.qf.hospital.system.util.JedisUtil;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.SerializationUtils;

import java.io.Serializable;
import java.util.Collection;

/**
 * 重写Shiro默认的SessionDAO,从之前和Map交互,修改为和Redis交互
 */
@Component
public class RedisSessionDAO extends AbstractSessionDAO {

    @Autowired
    private JedisUtil jedisUtil;

    private static final String USER_REDIS_PREFIX = "USER:";

    private static final int TIMEOUT = 60*10;


    // 登录成功后，将信息存储到Redis
    // session是存储到Redis的value
    // 存储到Redis中的key是SessionId,通过Session获取
    @Override
    public Serializable doCreate(Session session) {
        System.out.println("向Redis存储session");
        //1. 生成SessionID
        Serializable sessionId = generateSessionId(session);   // UUID.randomUUID

        //2. 将sessionID和Session绑定到一起
        assignSessionId(session, sessionId);            // session.getId() == sessionId

        //3. 准备byte[]的key
        byte[] key = SerializationUtils.serialize(USER_REDIS_PREFIX + sessionId);

        //4. 准备byte[]的value
        byte[] value = SerializationUtils.serialize(session);

        //5. 存储到Redis中
        jedisUtil.setex(key,value,TIMEOUT);

        //6. 返回SessionID
        return sessionId;
    }

    // 从Redis读取Session信息
    @Override
    public Session doReadSession(Serializable sessionId) throws UnknownSessionException {
        System.out.println("读取Redis中的Session");
        //1. 封装byte[]的key
        byte[] key = SerializationUtils.serialize(USER_REDIS_PREFIX + sessionId);

        //2. 获取数据
        byte[] value = jedisUtil.get(key);

        //3. 如果session不为null,重新设置key的生存时间
        if(value != null){
            jedisUtil.expire(key,TIMEOUT);
        }
        Session session = (Session) SerializationUtils.deserialize(value);

        //4. 返回Session
        return session;
    }

    // 重置Redis的生存时间
    @Override
    public void update(Session session) throws UnknownSessionException {
        System.out.println("修改session信息");
        //1. 获取SessionId
        Serializable sessionId = session.getId();

        //2. 封装key
        byte[] key = SerializationUtils.serialize(USER_REDIS_PREFIX + sessionId);

        //3. 封装value
        byte[] value = SerializationUtils.serialize(session);

        //4. 存储到Redis
        jedisUtil.setex(key,value,TIMEOUT);
    }

    // 删除Redis的key
    @Override
    public void delete(Session session) {
        //1. 获取sessionId
        Serializable sessionId = session.getId();
        //2. 封装key
        byte[] key = SerializationUtils.serialize(USER_REDIS_PREFIX + sessionId);
        //3. 调用del
        jedisUtil.del(key);
    }


    // 别管
    @Override
    public Collection<Session> getActiveSessions() {
        return null;
    }
}
